package com.googlecode.ozonbooksfinder.matefinder.actions
{
	import com.asfusion.mate.actionLists.IScope;
	import com.asfusion.mate.actionLists.ServiceHandlers;
	import com.asfusion.mate.actionLists.ServiceScope;
	import com.asfusion.mate.events.UnhandledFaultEvent;
	
	import flash.events.Event;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import com.asfusion.mate.core.*;
	
	use namespace mate;

	public class WebServiseHanglers extends ServiceHandlers
	{
		public function WebServiseHanglers(inheritedScope:IScope=null)
		{
			super(inheritedScope);
		}
		
		override protected function fireEvent(event:Event):void
		{	
			if((event.hasOwnProperty("token")) && (event ["token"] == token)) 
			{
				if(actions && actions.length > 0)
				{
					var currentScope:ServiceScope = 
						new ServiceScope(inheritedScope.event, debug, inheritedScope);
					currentScope.owner = this;
				
					if(event is FaultEvent)
					{
						currentScope.fault  = FaultEvent(event).fault;
					}
					else
					{
						if (event.hasOwnProperty("result"))
						{
							currentScope.result  = event ["result"];
						}
					}
					
					
					setScope(currentScope);
					runSequence(currentScope, actions);
				}
				else if(event is FaultEvent)
				{
					var faultEvent:UnhandledFaultEvent = 
						new UnhandledFaultEvent(UnhandledFaultEvent.FAULT);
					faultEvent.fault = FaultEvent(event).fault;
					faultEvent.headers = FaultEvent(event).headers;
					faultEvent.message = FaultEvent(event).message;
					faultEvent.token = FaultEvent(event).token;
					faultEvent.messageId = FaultEvent(event).messageId;
					inheritedScope.dispatcher.dispatchEvent(faultEvent);
				}
				dispatcher.removeEventListener(type,fireEvent);
			}
		}
	}
}